import {
	baseUrl,
	uploadUrl
} from './config.js'
import md5 from './md5.min.js'
export function ajax(url, data = {}, method = 'GET', header = {
	'content-type': 'application/x-www-form-urlencoded','token' : uni.getStorageSync('userinfo') }, type = 'base') {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			mask: true
		})
		let time = parseInt(new Date().getTime() / 1000)
		// if (data.secret == undefined) {
		// 	data.secret =  this.apikey + time + uni.getStorageSync('userinfo').user_id + uni.getStorageSync('userinfo')
		// }
		// data.time = time
		// data.token = ''
		// if (uni.getStorageSync('userinfo') != '') {
		// 	data.token = uni.getStorageSync('userinfo');
		// }
		uni.request({
			url: (type == 'base' ? baseUrl : uploadUrl) + url,
			data: data,
			method: method,
			header: header,
			success(res) {
				// console.log(res)
				uni.hideLoading()
				uni.stopPullDownRefresh()
				if (res.data.code == 1) {
					resolve(res.data);
				} else {
					// token过期
					if (res.data.msg == '请先登录') {
						uni.showToast({
							title: '请先登录',
							icon: 'none',
							duration: 3000
						})
						uni.clearStorageSync()
						setTimeout(function() {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						}, 1500)
						return
					}
					uni.showToast({
						title: res.data.msg || '连接服务器失败,请稍后重试!',
						icon: 'none',
						duration: 3000
					})
					reject(res.data)
				}
			},
			fail(e) {
				uni.showToast({
					title: '连接失败,请检查网络后再试!',
					icon: 'none'
				})
				uni.hideLoading()
			}
		})
	})
}
